package org.paradise.dms.web.action.sysmgr;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.directwebremoting.annotations.RemoteProxy;
import org.paradise.dms.pojo.AllocationInfo;
import org.paradise.dms.services.SystemMgrService;
import org.paradise.dms.web.action.DMSBaseAction;
import org.paradise.dms.web.tools.DWRPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 对应系统AllocationInfo表的action
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 Apr 11, 2009 7:45:00 PM 李双江（paradise.lsj@gmail.com）created
 */
@Service
@RemoteProxy(name = "systemPreassignTypeAction")
public class SystemPreassignTypeAction extends DMSBaseAction {
	private static Logger log = Logger.getLogger(SystemPreassignTypeAction.class);
	/**
	 * 
	 */
	private static final long serialVersionUID = 4600615626388068599L;

	@Autowired
	private SystemMgrService systemMgrService;

	/**
	 * 
	 * Description:分页查询预分配type
	 * 
	 * @Version1.0 Apr 11, 2009 8:09:51 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param pageNum
	 * @param totalCount
	 * @param session
	 * @return
	 */

	@SuppressWarnings("static-access")
	public JSONArray listAllPreassignTypeByPage(int pageNum, int totalCount, HttpSession session) {
		List<AllocationInfo> preidList = new ArrayList<AllocationInfo>();
		int totalRow = totalCount;
		DWRPage page = new DWRPage();
		page.setPageProperties(totalRow, 10);
		page.setCurrentPage(pageNum);
		session.setAttribute("page", page);
		int cp = (page.getCurrentPage() - 1) * 10;
		int ps = page.getPageSize();
		preidList = systemMgrService.listAllPreassignTypeByPage(ps, cp);
		if (preidList != null & preidList.size() > 0) {
			JSONArray preidjson = new JSONArray().fromObject(preidList);
			return preidjson;
		}
		return null;
	}

	/**
	 * 
	 * Description:查询预分配type 的总行数
	 * 
	 * @Version1.0 Apr 11, 2009 8:09:43 PM 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public int getAllPreassignTypeRows() {
		return systemMgrService.getAllPreassignTypeRows();
	}

	/**
	 * 
	 * Description:返回页面分页对象
	 * 
	 * @Version1.0 2009-3-28 上午11:13:26 李双江（paradise.lsj@gmail.com）创建
	 * @param session
	 * @return
	 */
	@SuppressWarnings("static-access")
	public JSONObject getPage(HttpSession session) {
		JSONObject joa = new JSONObject().fromObject((DWRPage) session.getAttribute("page"));
		return joa;
	}

	/**
	 * 
	 * Description: 根据id删除一条preassignType
	 * 
	 * @Version1.0 Apr 11, 2009 7:46:19 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param allocationid
	 * @return
	 */
	public String deleltePreassignTypeByID(String allocationid) {
		try {
			return systemMgrService.deleltePreassignTypeByID(allocationid);
		} catch (DaoException e) {
			log.info("DMS_error:根据id删除一条preassignType有误！");
			log.info("DMS_error:错误原因！" + e);
		}
		return "删除出错!";
	}

	/**
	 * 
	 * Description:增加一条preassigntype
	 * 
	 * @Version1.0 Apr 11, 2009 8:46:55 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param preassigntypename
	 * @return
	 */
	public String addPreassignType(String preassigntypename,String starttime,String endtime) {
		return systemMgrService.insertPreassignType(preassigntypename,starttime, endtime);
	}

	/**
	 * 
	 * Description: 更新一条preassign类型
	 * 
	 * @Version1.0 Apr 22, 2009 2:02:37 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param preassignStr
	 * @return
	 */
	public String updatePreassignType(String preassignStr) {
		AllocationInfo ai = new AllocationInfo();
		String[] prestr = preassignStr.split(",");
		ai.setAllocationid(prestr[0]);
		ai.setAllocationname(prestr[1]);
		return systemMgrService.updatePreassignType(ai);
	}

	public SystemMgrService getSystemMgrService() {
		return systemMgrService;
	}

	public void setSystemMgrService(SystemMgrService systemMgrService) {
		this.systemMgrService = systemMgrService;
	}

}
